<template>
	<div class="hy-container">
		<HyPageHeader :title="`${title}抽取`" :buttonData="buttonData">
			<div v-if="routerParams.operateType !== 'view'">
				<BasicInfo
					:routerParams="routerParams"
					v-if="routerParams.operateType !== 'view'"
					ref="basicRef"
					:reviewAppData="reviewAppData"
					:detailData="basicData"
				/>
				<div class="mt-5">
					<Ways :routerParams="routerParams" :detailData="waysData" @sendcheckData="getCheckData" ref="waysRef" />
				</div>
				<div class="mt-5" v-if="checkData.includes(0)">
					<ExpertWay
						:expertSpecifiedData="expertSpecifiedData"
						:expertAvoidanceData="expertAvoidanceData"
						:detailData="expertWayData"
						:routerParams="routerParams"
						ref="expertWayRef"
					/>
				</div>
				<div class="mt-5" v-if="checkData.includes(1)">
					<InstitutionWay
						:detailData="institutionWayData"
						:institutionAvoidanceData="institutionAvoidanceData"
						:routerParams="routerParams"
						ref="institutionWayRef"
					/>
				</div>
			</div>
			<div v-else>
				<Detail
					:basicData="basicData"
					:expertWayData="expertWayData"
					:extractResultData="extractResultData"
					:expertAvoidanceData="expertAvoidanceData"
					:routerParams="routerParams"
					:reviewAppData="reviewAppData"
					:institutionWayData="institutionWayData"
					:expertSpecifiedData="expertSpecifiedData"
					:institutionAvoidanceData="institutionAvoidanceData"
					:institutionListData="institutionListData"
					:expertListData="expertListData"
					@handleRefresh="handleRefresh"
				/>
			</div>
		</HyPageHeader>
	</div>
</template>
<script setup lang="ts">
import BasicInfo from './components/BasicInfo/index.vue';
import Ways from './components/Ways/index.vue';
import ExpertWay from './components/ExpertWay/index.vue';
import InstitutionWay from './components/InstitutionWay/index.vue';
import HyPageHeader from '/@/components/HyPageHeader/index.vue';
import { meetingAddApi, meetingEditApi } from '/@/api/expertDatabase/extract';
import { ElLoading } from 'element-plus';
import mittBus from '/@/utils/mitt';
import { useDetail } from './hooks/useDetail';
import Detail from './components/Detail/index.vue';

const {
	basicData,
	institutionAvoidanceData,
	routerParams,
	expertWayData,
	expertSpecifiedData,
	institutionWayData,
	expertAvoidanceData,
	extractResultData,
	reviewAppData,
	title,
	waysData,
	expertListData,
	institutionListData,
	getDetailData,
} = useDetail();

const checkData = ref<number[]>([]);
const getCheckData = (data: any) => {
	checkData.value = data;
};
const basicRef = ref();
const waysRef = ref();
const expertWayRef = ref();
const institutionWayRef = ref();
const route = useRoute();
const handleSave = async () => {
	let params: any = {};
	//校验基础表单必填项
	const resutl1 = await basicRef.value.check();
	if (!resutl1) return;
	params = {
		baseInfo: basicRef.value.getData().baseInfo,
		reviewAppList: basicRef.value.getData().reviewAppList,
	};
	//修改,只修改基本信息
	if (routerParams.value.operateType === 'edit') {
		const loading = ElLoading.service({
			lock: true,
			text: '提交中...',
		});
		meetingEditApi(basicRef.value.getData().baseInfo)
			.then(() => {
				mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
			})
			.finally(() => {
				loading.close();
			});
	}
	//修改,只修改基本信息
	if (routerParams.value.operateType === 'add') {
		//校验抽取方式是否必填
		const resutl2 = await waysRef.value.submitForm();
		if (!resutl2) return;
		const extractionModeList = waysRef.value.form.extractionModeList;
		params = {
			...params,
			extractionMode: {
				extractionModeList,
			},
		};

		//如果是专家抽取,校验专家表单是否必填
		if (extractionModeList.includes(0)) {
			const resutl3 = await expertWayRef.value.check();
			if (!resutl3) return false;
			params = {
				...params,
				extractionMode: {
					...params.extractionMode,
					...expertWayRef.value.getData(),
				},
			};
		}
		//如果是机构抽取,校验机构表单是否必填
		if (extractionModeList.includes(1)) {
			const resutl4 = await institutionWayRef.value.check();
			if (!resutl4) return;
			params = {
				...params,
				extractionMode: {
					...params.extractionMode,
					...institutionWayRef.value.getData(),
				},
			};
		}
		const loading = ElLoading.service({
			lock: true,
			text: '提交中...',
		});
		meetingAddApi(params)
			.then(() => {
				mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
			})
			.finally(() => {
				loading.close();
			});
	}
};
const buttonData = computed(() => {
	if (routerParams.value.operateType === 'view') return [];
	return [{ name: '保存', class: 'white-button', click: () => handleSave() }];
});
//刷新
const handleRefresh = () => {
	getDetailData(routerParams.value.id);
};
</script>
<style scoped lang="scss"></style>
